package com.ailk.kbi.kbirazor.model
{
	import com.ailk.kbi.kbirazor.view.ResizableTitleWindow;
	import com.ailk.kbi.kbirazor.vo.DimVO;
	import com.ailk.kbi.kbirazor.vo.KpiVO;
	import com.ailk.kbi.kbirazor.vo.MindMapVO;
	import com.ailk.kbi.kbirazor.vo.TimeRangeVO;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.List;
	
	
	/**脑图XML变更事件*/
	[Event(name="mindMapXMLChange", type="flash.events.Event")]	
	
	/**时间范围变更事件*/
	[Event(name="selectableRangeChange", type="flash.events.Event")]	
	
	/**KPI值变更事件*/
	[Event(name="kpiValuesChange", type="flash.events.Event")]
	
	
	[Bindable]
	public class KbrModel  extends EventDispatcher
	{
		/**存储脑图列表*/
		public var mindMapList:ArrayCollection;
		
		/**当前脑图VO*/
		public var mindMapVO:MindMapVO;
		
		public var nodeSelectedFlag:Boolean = false;
		public var nodeSelectedNum:int = 0;
		
		/**当前脑图XML内容*/
		private var _mindMapXML:XML;
		
		/**
		 * KPI ID数组
		 */
		public var kpiIds:ArrayCollection;
		
		public var _warnKpiIds:ArrayCollection;
		
		public var currentDate:String;
		
		public var thresholdSetWindowFlag:Boolean = false;
		public var thresholdSetXML:XML;
		
		public var descTitleWindowFlag:Boolean = false;
		public var descDataXML:XML;
		public var descCondition:String = "";
		
		public function get warnKpiIds():ArrayCollection{
			return _warnKpiIds;
		}
		
		public function set warnKpiIds(value:ArrayCollection):void{
			_warnKpiIds = value;
			dispatchEvent(new Event("getKpiIdsComplete"));
		}
		
		
		
		public function set kpiValues(value:ArrayCollection):void{
			if(value!=null){
				for each(var kpiVO:KpiVO in  value){
					updateKpiValue(kpiVO);
				}
				dispatchEvent(new Event("kpiValuesChange"));
			}
		}
		
		private function updateKpiValue(kpiVO:KpiVO):void{
			for each(var kpiXML:XML in _mindMapXML..kpi.(@id == kpiVO.kpiId)){				
				kpiXML.@kpiValue = kpiVO.kpiValue;
				kpiXML.@kpiValue1 = kpiVO.kpiValue1;
				kpiXML.@kpiValue2 = kpiVO.kpiValue2;
				kpiXML.@riseValue = kpiVO.riseValue;
				kpiXML.@opTime = kpiVO.opTime;
				kpiXML.@cityName = kpiVO.cityName;
				kpiXML.@cityId = kpiVO.cityId;
				kpiXML.@areaId = kpiVO.areaId;
			}
		}
		
		public function get mindMapXML():XML{
			return _mindMapXML;
		}
		
		public function set mindMapXML(value:XML):void{
			if(value != _mindMapXML){
				_mindMapXML = value;
				
				/*得到KPI ID的列表*/
				kpiIds = new ArrayCollection()
				for each(var kpiXML:XML in _mindMapXML..kpi){
					kpiXML.toString()
					if(kpiXML.hasOwnProperty('@id')){
						kpiIds.addItem(kpiXML.@id.toString());
					}
				}
				
				timeSQL = null;
				
				/*获取配置信息*/
				if(_mindMapXML.hasOwnProperty("config")){
					var configXML:XML = XML(_mindMapXML.config);
					/*获取时间范围信息*/
					if(configXML.hasOwnProperty("timeSQL")){
						timeSQL = configXML.timeSQL;
					}					
				}				
				
				dispatchEvent(new Event("mindMapXMLChange"));
//				dispatchEvent(new Event("getKpiIdsComplete"));
			}
		}
		
		/**时间范围SQL*/
		public var timeSQL:String;
		
		/**时间范围*/
		private var _timeRangeVO:TimeRangeVO;
		
		/**时间选择框可选择时间范围*/
		private var _selectableRange:Object;
		
		public function get timeRangeVO():TimeRangeVO{
			return _timeRangeVO;
		}
		
		public function get selectableRange():Object{
			return _selectableRange;
		}
		
		public function set timeRangeVO(value:TimeRangeVO):void{
			if(value != _timeRangeVO){
				_timeRangeVO = value;	
				
				/*得到時間維表的結果*/
				if(_timeRangeVO){
					var fromDate:Date;
					var toDate:Date;
					if(_timeRangeVO.from.length == 6){
						/*月格式*/
						fromDate = string2Month(_timeRangeVO.from);
						toDate = string2Month(_timeRangeVO.to);
					}
					else{
						/*日格式*/
						fromDate = string2Date(_timeRangeVO.from);
						toDate = string2Date(_timeRangeVO.to);
					}
					_selectableRange = {rangeStart:fromDate,rangeEnd:toDate};
				}
				
				dispatchEvent(new Event("selectableRangeChange"));
			}
		}
		
		
		
		public function KbrModel()
		{
		}
		
		public function anchorChange():void{
			dispatchEvent(new Event("anchorChange"));
		}
		
		public function clickFocus():void {
			dispatchEvent(new MouseEvent("clickChange"));
		}
		
		private static var _instance:KbrModel;
		
		
		
		public static function getInstance():KbrModel{
			if(_instance==null){
				_instance= new KbrModel();
			}
			
			return _instance;
		}
		
		/*把字符串转为日期*/
		private function string2Date(value:String,offset:int=0):Date{
			var year:int =	int(value.substr(0,4));
			var month:int =	int(value.substr(5,2)) - 1;
			var day:int = int(value.substr(8,2)) + offset;
			var date:Date = new Date(year,month,day);
			return date;
		}
		
		/*把字符串转为月的日期*/
		private function string2Month(value:String):Date{
			var year:int =	int(value.substr(0,4));
			var month:int =	int(value.substr(4,2)) - 1;
			var date:Date = new Date(year,month);
			return date;
		}
	}
}